﻿using System;
using System.IO;
using System.Net;
using System.Text;

namespace BattleNet.DataAccess.UnitTests.Providers.Web
{
    class TestWebRequestCreate : IWebRequestCreate
    {
        static WebRequest _nextRequest;
        static object lockObject = new object();

        static public WebRequest NextRequest
        {
            get { return _nextRequest; }
            set
            {
                lock (lockObject)
                {
                    _nextRequest = value;
                }
            }
        }

        /// <summary>See <see cref="IWebRequestCreate.Create"/>.</summary>
        public WebRequest Create(Uri uri)
        {
            return new TestWebRequest(uri.ToString());
        }

        /// <summary>Utility method for creating a TestWebRequest and setting
        /// it to be the next WebRequest to use.</summary>
        /// <param name="response">The response the TestWebRequest will return.</param>
        public static TestWebRequest CreateTestRequest(string response)
        {
            TestWebRequest request = new TestWebRequest(response);
            NextRequest = request;
            return request;
        }
    }

    class TestWebRequest : WebRequest
    {
        private readonly string _uri;

        public override string Method { get; set; }
        public override string ContentType { get; set; }
        public override long ContentLength { get; set; }

        /// <summary>Initializes a new instance of <see cref="TestWebRequest"/>
        /// with the response to return.</summary>
        public TestWebRequest(string response)
        {
            _uri = response;
        }

        /// <summary>Returns the request contents as a string.</summary>
        public string ContentAsString()
        {
            return "Source";
        }

        /// <summary>See <see cref="WebRequest.GetRequestStream"/>.</summary>
        public override Stream GetRequestStream()
        {
            return new MemoryStream();
        }

        /// <summary>See <see cref="WebRequest.GetResponse"/>.</summary>
        public override WebResponse GetResponse()
        {
            if(_uri != "test://exists.com/")
            {
                throw new WebException();
            }        
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes("Source"));
            return new TestWebReponse(ms);
        }
    }

    class TestWebReponse : WebResponse
    {
        Stream responseStream;

        /// <summary>Initializes a new instance of <see cref="TestWebReponse"/>
        /// with the response stream to return.</summary>
        public TestWebReponse(Stream responseStream)
        {
            this.responseStream = responseStream;
        }

        /// <summary>See <see cref="WebResponse.GetResponseStream"/>.</summary>
        public override Stream GetResponseStream()
        {
            return responseStream;
        }
    }
}
